package com.nbgs.demo.service.impl;

import com.nbgs.demo.dao.AutoGenerateInfoMapper;
import com.nbgs.demo.info.PartConsumptionInfo;
import com.nbgs.demo.info.PartInsertConsumptionInfo;
import com.nbgs.demo.service.AutoGenerateInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.PropertyEditorManager;
import java.util.List;

@Service
@Slf4j
public class AutoGenerateInfoServiceImpl implements AutoGenerateInfoService {

    @Autowired
    private AutoGenerateInfoMapper autoGenerateInfoMapper;


    @Override
    public void generateInfo(String time) {
        String[] times = time.split("-");

        int year = Integer.parseInt(times[0]);
        int month = Integer.parseInt(times[1]);

        log.info("当前时间参数："+year+","+month);

        List<PartConsumptionInfo> partConsumptionInfos = autoGenerateInfoMapper.selectPartInfo();

        log.info(partConsumptionInfos.toString());

        for (PartConsumptionInfo partConsumptionInfo:partConsumptionInfos) {

            String preTimeInfo = partConsumptionInfo.getConsumptionMonth();

            String[] preTimes = preTimeInfo.split(",");

            int preYear = Integer.parseInt(preTimes[0]);
            int preMonth = Integer.parseInt(preTimes[1]);

            log.info("上一次缴费时间："+preYear+","+preMonth);

            PartInsertConsumptionInfo partInfo = null;

            if((month==1&&preMonth==11)||(month==2&&preMonth==12)||(month-2==preMonth)){

                partInfo = new PartInsertConsumptionInfo();

                int cardId = partConsumptionInfo.getCardId();

                log.info("满足要求的cardID："+cardId);

                partInfo.setCardId(cardId);

                partInfo.setConsumptionMonth(times[0]+","+times[1]);

                partInfo.setConsumptionStart(partConsumptionInfo.getConsumptionEnd());

                autoGenerateInfoMapper.autoGenerateInfo(partInfo);
            }

        }

    }
}
